package com.leilei.greendao.ui;

import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.leilei.greendao.R;
import com.leilei.greendao.db.GreenDaoManager;
import com.leilei.greendao.mode.Person;
import com.leilei.greendao.util.Util;

import java.util.List;

/**
 * Created by Administrator on 2017/9/24 0024.
 */

public class RecyclerActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private BaseItemDraggableAdapter<Person, BaseViewHolder> mAdapter;
    private List<Person> mList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);
        init();
        initData();
    }

    private void init() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new BaseItemDraggableAdapter<Person, BaseViewHolder>(R.layout.item_recyclerview, mList) {
            @Override
            protected void convert(BaseViewHolder helper, Person item) {
                helper.setText(R.id.textview1, mList.get(helper.getLayoutPosition()).getId() + "");
                helper.setText(R.id.textview2, mList.get(helper.getLayoutPosition()).getDate() + "");
                helper.setText(R.id.textview3, mList.get(helper.getLayoutPosition()).getStep() + "");
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(RecyclerActivity.this, "你选择了第" + position + "项，内容:" + mList.get(position).getId() + "||" + mList.get(position).getDate(), Toast.LENGTH_LONG).show();
            }
        });
        ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
        // 开启拖拽
        mAdapter.enableDragItem(itemTouchHelper, R.id.line2, true);
        mAdapter.setOnItemDragListener(new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {

            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {

            }
        });
        // 开启滑动删除
        mAdapter.enableSwipeItem();
        mAdapter.setOnItemSwipeListener(new OnItemSwipeListener() {
            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {

            }

            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
                final long start_time = System.currentTimeMillis();
                GreenDaoManager.getInstances().delete(mList.get(pos).getId());
                long end_time = System.currentTimeMillis();
                Util.showSnackBar(mRecyclerView, "删除成功,耗时：" + (end_time - start_time) + "ms");
            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {

            }
        });
    }

    private void initData() {
        final long start_time1 = System.currentTimeMillis();
        mList =  GreenDaoManager.getInstances().query();
        for (int i = 0; i < mList.size(); i++) {
            String date = mList.get(i).getDate();
            Log.e("查询数据", "第" + i + "数据:" + date);
        }
        long end_time1 = System.currentTimeMillis();
        Util.showSnackBar(mRecyclerView, "查询成功," + mList.size() + "条数据,耗时：" + (end_time1 - start_time1) + "ms");
        mAdapter.setNewData(mList);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
